package com.htbb.common.mq;

import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.htbb.common.util.DateUtil;
import com.htbb.model.Protocol;
import com.htbb.model.TcAcctIsu;
import com.htbb.service.OrderService;

/**
 * @项目名称：cvp
 * @类名称：MqClientUtil
 * @类描述：消息处理
 * 创建人：wuyongde  
 * 创建时间：2017-4-12 下午03:54:41    
 * 修改人：wuyongde   
 * 修改时间：2017-4-12 下午03:54:41 
 * @修改备注：
 * @version 1.0
 */
public class MqClientUtil {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
		
	private static MqClientUtil mcu = new MqClientUtil();

	public static MqClientUtil getInstance() {
		if (mcu == null) {
			mcu = new MqClientUtil();
		}
		return mcu;
	}

	/**
	 * 处理通讯平台发送过来的数据包
	 * @param param
	 * @param message JSON数据包
	 * @throws Exception
	 */
	public void messageReceived(int type,String message) throws Exception {
		try {
			logger.info(DateUtil.getSQLDate()+"【接收到通讯平台发送消息】"+message);
			
			OrderService orderService = Config.orderService;
			Protocol protocol = JSON.parseObject(message, Protocol.class);
			switch (type) {
				/***************************************协议接入******************************************/
				//下订单
				case 1:
					if(StringUtils.isEmpty(protocol.getUserId())){
						logger.info("UserId为null，抛弃不进行处理");
					}else if(StringUtils.isEmpty(protocol.getIsuId())){
						logger.info("IsuId为null，抛弃不进行处理");
					}else{
						//保存订单
						orderService.saveOrder(protocol);
					}
					break;
					
				//订单状态变化通知
				case 2:
					if(StringUtils.isEmpty(protocol.getUserId())){
						logger.info("UserId为null，抛弃不进行处理");
					}else if(StringUtils.isEmpty(protocol.getIsuId())){
						logger.info("IsuId为null，抛弃不进行处理");
					}else{
						//更新订单状态
						orderService.updateOrderStatus(protocol);
					}
					break;
					
					//预支付
				case 3:
					if(StringUtils.isEmpty(protocol.getIsuId())){
						logger.info("IsuId为null，抛弃不进行处理");
					}else{
						//插入支付记录
						orderService.insertTcPay(protocol);
					}
					break;
					
					//订单提交标志
				case 4:
					//更新bk2字段值为commit
					String []isuArr = protocol.getIsuId().split(",");
					List<String> list = new LinkedList<String>();
					for(int i=0;i<isuArr.length;i++){
						list.add(isuArr[i]);
					}
					orderService.updateBk2(list);
					break;
	
				default:
					logger.info("收到与业务无关数据包，抛弃不进行处理");
					break;
			}
			protocol = null;
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}

	}

	public static void main(String[] args) {
		MqClientUtil mqClientUtil = new MqClientUtil();
		
		String message = "{payId:111112,isuId:\"123456\",prodName:\"123456\",payAmt:\"123456\",appType:\"123456\",notifyUrl:\"123456\"}";
		
		try {
			mqClientUtil.messageReceived(3,message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
